layui.use('table', function(){
	var table = layui.table;
	var renderTable = function(){
		table.render({
			elem: '#menu-table'
			,url: '/admin/sys/attachment/ajaxAttList' //数据接口
			,page: true //开启分页
			,cols: [[ //表头
//			{field: 'id', title: 'ID', width:80, sort: true, fixed: 'left'}
			{type: 'numbers', title: '序号'},
			{field: 'originName', title: '文件名', width: 150, sort: true}
			,{field: 'type', title: '文件类型', width: 120, sort: true}
			,{field: 'fileSize', title: '文件大小', width: 110, sort: true} 
			,{field: 'relativePath', title: '映射路径', sort: true}
			,{field: 'uploadPath', title: '上传路径', sort: true}
			,{field: 'downloadCounts', title: '下载次数', width: 110, sort: true}
			,{field: 'uploadTime', title: '上传时间', width: 170, sort: true}
			,{templet: '#auth-state', width: 120, align: 'center', title: '操作'}
			]],
			done: function () {
				layer.closeAll('loading');
			}
			,response: {
			statusName: 'status' //规定数据状态的字段名称，默认：code
			,statusCode: 200 //规定成功的状态码，默认：0
			,msgName: 'msg' //规定状态信息的字段名称，默认：msg
//			,countName: 'total' //规定数据总数的字段名称，默认：count
			,dataName: 'data' //规定数据列表的字段名称，默认：data
			} 
		});
	}
	renderTable();
	//监听工具条
	table.on('tool(menu-table)', function (obj) {
		var data = obj.data;
		var layEvent = obj.event;
		if (layEvent === 'del') {
			layer.confirm(
					'确定要删除附件【'+data.originName+'】吗？删除后可能会影响到其他模块，请谨慎操作！\
					<br><input name="delFile" class="ace ace-checkbox-2" type="checkbox" />\
					<span class="lbl"> 同时删除附件文件</span>', 
					{icon: 3, title:'提示'},
					function(index){
						var delFile = false;
						if($('input[name=\'delFile\']').is(':checked')) 
							delFile = true;
						deleteAtt(data.id, delFile);
						layer.close(index);
					}
				);
		}else if(layEvent === 'download'){
			//下载附件
			var a = document.createElement('a');
		    var event = new MouseEvent('click');
		    a.download = obj.data.originName;
		    a.href = obj.data.relativePath;
		    a.dispatchEvent(event);
		    //下载次数加1
			$.ajax({
				url: '/admin/sys/attachment/setDownloadCounts',
				data: {
					"id": obj.data.id,
					"count": parseInt(obj.data.downloadCounts)+1
				},
				type: 'POST',
				dataType: 'JSON',
				success: function(){
					renderTable();
				},
				error: function(){
					renderTable();
				}
			});
		}
	});

	//删除附件
	function deleteAtt(id, delFile){
		layer.load();//加载弹窗
		var form = layui.form;
		$.ajax({
			type: "post",
			url : "/admin/sys/attachment/deleteAtt",
			dataType:'json',
			data: {
				"id": id,
				"delFile": delFile
			},
			success: function(obj){
				var delSuccess = obj.data;
				if(delSuccess){
					gritterSuccess(delSuccess);
				}else{
					gritterError('删除失败');
				}
				renderTable();
			},
			error: function(){
				gritterError(info.noAuth);
				renderTable();
			}
		});
	}

});
//layui.config({
//base: '/static/admin/treetable/module/'
//}).extend({
//treetable: 'treetable-lay/treetable'
//}).use(['table'], function () {

//var table = layui.table;

//var renderTable = function(){
//table.render({
//elem: '#menu-table'
//,url: '/admin/sys/attachment/ajaxAttList' //数据接口
//,page: true //开启分页
//,id:'id'
//,cols: [[ //表头
//{field: 'id', title: 'ID', width:80, sort: true, fixed: 'left'}
//,{field: 'originName', title: '文件名', sort: true}
//,{field: 'type', title: '文件类型', sort: true}
//,{field: 'fileSize', title: '文件大小', sort: true} 
//,{field: 'sign', title: '映射路径', sort: true}
//,{field: 'experience', title: '上传路径', sort: true}
//,{field: 'score', title: '下载次数', sort: true}
//,{templet: '#auth-state', width: 180, align: 'center', title: '操作'}
//]],
//done: function () {
//layer.closeAll('loading');
//}
//});
//}
//renderTable();
//});

//var openIndex;//记录打开的弹窗的index值，方便添加、修改后关闭该弹窗
////layer弹出框
//function confirmOperationMenu(data, title) {
//var form = layui.form;
//openIndex = layer.open({
//type: 1,
//title: title,
//closeBtn: 1,//0表示显示，1、2表示不同风格的关闭按钮
//shadeClose: true,
//content: operationDialog(data, title),
//success:function(index, layero){
//form.render();//重新加载，否则单选、复选、下拉选不显示
//}
//});
//}

////删除菜单
//function deleteMenu(id){
//layer.load();//加载弹窗
//var form = layui.form;
//$.ajax({
//type: "post",
//url : "/admin/sys/menu/delete",
//dataType:'json',
//data: {
//"id":id
//},
//success: function(data){
//var delSuccess = data.data;
//if(delSuccess){
//gritterSuccess(delSuccess);
//$('#btn-refresh').trigger('click');
//}else{
//gritterError('删除失败');
//}
//},
//error: function(){
//gritterError('删除失败');
//}
//});
//}

